package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContext;
import com.itheima.common.R;
import com.itheima.common.RedisResultKey;
import com.itheima.entity.Employee;
import com.itheima.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;


/**
 * @author 王磊
 * @date 2022/5/5
 */
@RestController
@RequestMapping("/employee")
@Slf4j
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 员工登入
     *
     * @param employees
     * @return
     */
    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request,@RequestBody Employee employees) {
        //md5加密
        String password = employees.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();

        //根据用户名查询数据库
        wrapper.eq(Employee::getUsername, employees.getUsername());

        Employee employee = employeeService.getOne(wrapper);
        //判断用户名是否存在
        if (employee == null) {
            return R.error("用户名不存在");
        }
        //判断密码是否正确
        if (!employee.getPassword().equals(password)) {
            return R.error("密码错误");
        }
        //判断账号是否禁用
        if (employee.getStatus() == 0) {
            return R.error("账号已禁用");
        }
        request.getSession().setAttribute("employee",employee);
        BaseContext.setCurrentId(employee.getId());
        return R.success(employee);
    }

    /**
     * 员工退出
     * @return
     */
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
       request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }
    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        Page<Employee> pages = new Page(page, pageSize);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(!StringUtils.isEmpty(name),Employee::getName, name);
        wrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(pages, wrapper);
        return R.success(pages);
    }

    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes(StandardCharsets.UTF_8)));
        employeeService.save(employee);
        return R.success("添加成功");
    }

    /**
     * 修改员工信息
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public R<Employee> update(@PathVariable Long id) {
        System.out.println(id);
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getId, id);
        Employee employee = employeeService.getOne(wrapper);
        System.out.println(employee);
        return R.success(employee);
    }

    @PutMapping
    public R<String> update(@RequestBody Employee employee) {
        employeeService.updateById(employee);
        return R.success("修改成功");
    }

}
